home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 140_01.zip / CLOCK.C < prev    next >
Text File  |  1993-06-26  |  6KB  |  258 lines

  1. #include    CLOCK.H
  2.  
  3. #define    CENTURY    19        /* 20th Century at the moment */
  4.  
  5. /*
  6.    By Bill Bolton,
  7.    Software Tools,
  8.    P.O. Box 80,
  9.    Newport Beach,
  10.    NSW, 2106,
  11.    AUSTRALIA
  12.  
  13.    Source Address TCY-396
  14.    Phone (+61 2) 997-1018
  15.  
  16.    Version 1.0 for Mountain Hardware 100,000 day clock 11/5/81
  17.    Version 1.1 for Godbout Systsen Support 1 clock 19/1/82 */
  18.  
  19.  
  20. /* date(srt,format) will fill a string with date formatted as follows:
  21.  
  22.     format = 0    "May 11, 1981"
  23.     format = 1    "Monday, May 11, 1981"
  24.     format = 2    "11/5/1981"
  25.     format = 3    "Monday, 11/5/1981"
  26.     format = 4    "11/5/81"
  27.     format = 5    "Monday, 11/5/81"
  28. */
  29.  
  30. date(str,format)
  31.  
  32. char    *str;        /* pointer to date string */
  33. int    format;        /* format identifier */
  34.  
  35. {
  36.     char    wname[10];    /* string for week day proper name */
  37.     char    mname[12];    /* string for month proper name */
  38.     int    year[1];    /* year, range 1978 to ????? */
  39.     int    month[1];    /* month of the year, range 1 to 12 */
  40.     int    mday[1];    /* day of the current month, range 0 to 31 */
  41.     int    wday[1];    /* day of the week, range 0 to 6 */ 
  42.  
  43.     if (get_date(year,month,mday,wday)){
  44.         printf("No clock board present in system\07\n");
  45.         return(-1);
  46.     } 
  47.     name_month(mname,month);
  48.     name_week(wname,wday);
  49.     switch(format){
  50.     case 0:
  51.         sprintf(str,"%s %d, %d%d",mname,*mday,CENTURY,*year);
  52.         return(0);
  53.     case 1:
  54.         sprintf(str,"%s, %s %d, %d%d",wname,mname,*mday,CENTURY,*year);
  55.         return(0);
  56.     case 2:
  57.         sprintf(str,"%d/%d/%d%d",*mday,*month,CENTURY,*year);
  58.         return(0);
  59.     case 3:
  60.         sprintf(str,"%s, %d/%d/%d%d",wname,*mday,*month,CENTURY,*year);
  61.         return(0);
  62.     case 4:
  63.         sprintf(str,"%d/%d/%d",*mday,*month,*year);
  64.         return(0);
  65.     case 5:
  66.         sprintf(str,"%s, %d/%d/%d",wname,*mday,*month,*year);
  67.         return(0);
  68.     default:
  69.         printf("Date format argument ERROR !\07\n\n");
  70.         return(-1);
  71.     }
  72. }
  73.  
  74. /* get_date(year,month,mday,wday) provides the basic data for formatting
  75.    a date string, fetched from the clock board and converted to a useable
  76.    set of values */
  77.  
  78. int get_date(year,month,mday,wday)
  79.  
  80. int    *year;        /* pointer to current year */
  81. int    *month;        /* pointer to current month */
  82. int    *mday;        /* pointer to day of the month */
  83. int    *wday;        /* pointer to day of the week */
  84.  
  85. {
  86.     if (inp(CDATA) == 0XFF )      /* no clock board present */
  87.         return(-1);
  88.     *year =  (read_digit(YEAR10) * 10) + read_digit(YEAR1);
  89.     *month = ((read_digit(MONTH10) & 1) * 10) + read_digit(MONTH1);
  90.     *mday = ((read_digit(DAY10) & 3) * 10) + read_digit(DAY1);
  91.     *wday = read_digit(WDAY);
  92.     outp(CLKCMD,0);
  93.     return(0);
  94. }
  95.  
  96.  
  97. int read_digit(address)
  98.  
  99. int    address;
  100.  
  101. {
  102.     int    instruct;
  103.  
  104.     instruct = address + CREAD;
  105.     outp(CLKCMD,instruct);
  106.     return (inp(CDATA));
  107. }
  108.  
  109. /* name_month(mname,month) fills a string with the name of the the
  110.    current month.
  111. */
  112.  
  113. int name_month(mname,month)
  114.  
  115. char    *mname;        /* pointer to month name string */
  116. int    *month;        /* pointer to current month */
  117.  
  118. {
  119.     switch(*month){
  120.     case 1:
  121.         strcpy(mname,"January");
  122.         return(0);
  123.     case 2:
  124.         strcpy(mname,"February");
  125.         return(0);
  126.     case 3:
  127.         strcpy(mname,"March");
  128.         return(0);
  129.     case 4:
  130.         strcpy(mname,"April");
  131.         return(0);
  132.     case 5:
  133.         strcpy(mname,"May");
  134.         return(0);
  135.     case 6:
  136.         strcpy(mname,"June");
  137.         return(0);
  138.     case 7:
  139.         strcpy(mname,"July");
  140.         return(0);
  141.     case 8:
  142.         strcpy(mname,"August");
  143.         return(0);
  144.     case 9:
  145.         strcpy(mname,"September");
  146.         return(0);
  147.     case 10:
  148.         strcpy(mname,"October");
  149.         return(0);
  150.     case 11:
  151.         strcpy(mname,"November");
  152.         return(0);
  153.     case 12:
  154.         strcpy(mname,"December");
  155.         return(0);
  156.     default:
  157.         printf("Month name ERROR ! Month was %d\n",*month);
  158.         return(-1);
  159.     }
  160. }
  161.  
  162. /* name_week(wname,wday) fills a string with the name of the current
  163.    week day.
  164. */
  165.  
  166. int name_week(wname,wday)
  167.  
  168. char    *wname;        /* pointer to week name string */
  169. int    *wday;        /* pointer to current week day */
  170.  
  171. {
  172.     switch(*wday){
  173.     case 0:
  174.         strcpy(wname,"Sunday");
  175.         return(0);
  176.     case 1:
  177.         strcpy(wname,"Monday");
  178.         return(0);
  179.     case 2:
  180.         strcpy(wname,"Tuesday");
  181.         return(0);
  182.     case 3:
  183.         strcpy(wname,"Wednesday");
  184.         return(0);
  185.     case 4:
  186.         strcpy(wname,"Thursday");
  187.         return(0);
  188.     case 5:
  189.         strcpy(wname,"Friday");
  190.         return(0);
  191.     case 6:
  192.         strcpy(wname,"Saturday");
  193.         return(0);
  194.     default:
  195.         printf("Weekday name ERROR !\n");
  196.         return(-1);
  197.     }
  198. }
  199.  
  200. /* time(str,format) fills a string with the time of day in the 
  201.    following formats :
  202.  
  203.     format 0     1800:15
  204.     format 1    18:00:15
  205.     format 2    18:00
  206. */
  207.  
  208. time(str,format)
  209.  
  210. char    *str;        /* string to fill with time */
  211. int    format;        /* flag for format of string */
  212.  
  213. {
  214.     int    t[6];
  215.  
  216.     if (read_clock(t)){
  217.         printf("No clock board present in system !\07\n");
  218.         return(-1);
  219.     } 
  220.     switch(format){
  221.     case 0:
  222.         sprintf(str,"%d%d%d%d:%d%d",t[0],t[1],t[2],t[3],t[4],t[5]);
  223.         return(0);
  224.     case 1:
  225.         sprintf(str,"%d%d:%d%d:%d%d",t[0],t[1],t[2],t[3],t[4],t[5]);
  226.         return(0);
  227.     case 2:
  228.         sprintf(str,"%d%d:%d%d",t[0],t[1],t[2],t[3]);
  229.         return(0);
  230.     default:
  231.         printf("Time of day format argument ERROR !\07\n\n");
  232.         return(-1);
  233.     }
  234. }
  235.  
  236. /* read_clock(t) fills an array with the time of day digits read from
  237.    the clock board
  238. */
  239.  
  240. int read_clock(t)
  241.  
  242. int    *t;        /* array to store clock digits */
  243.  
  244. {
  245.     int    ptr;    /* pointer into digit array */
  246.  
  247.     if (inp(CDATA) == 0XFF )    /* no clock board present */
  248.         return(-1);
  249.     t[0] = (read_digit(HOUR10) & 3);
  250.     t[1] = read_digit(HOUR1);
  251.     t[2] = (read_digit(MIN10) & 7);
  252.     t[3] = read_digit(MIN1);
  253.     t[4] = (read_digit(SEC10) & 7);
  254.     t[5] = read_digit(SEC1);
  255.     outp(CLKCMD,0);
  256.     return(0);
  257. }
  258.